/**
 * 销售合同
 * @author miaoyl
 * @date 2018/01/14
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_where = {},
        table_bottom_where = {
            GWCADOCNO: ''
        },
        vueData = {
            activeName: 'first'
        },
        cols = [[
            {type: 'checkbox', fixed: 'left'},
            {field: 'GWCADOCNO', width: 160, title: '申请单号', fixed: 'left', sort: true}
            ,{field: 'GWCADOCDATE', width: 160, title: '申请日期', fixed: 'left',sort: true}
            ,{field: 'GWCA005', width: 160, title: '合同编号', fixed: 'left', sort: true,
                search: 'default',searchOpts: {icon:true}}
            , {field: 'GWCA036', width: 160, title: '合同签订时间',search: 'default',searchOpts:{type:"date"}}
            , {field: 'GWCA055', width: 160, title: '客户单位', sort:true,
                search: 'default',searchOpts: {icon:true}}
            , {field: 'GWCA004', width: 160, title: '招标编号', sort:true,
                search: 'default',searchOpts: {icon:true}
            }
            , {field: 'GWGC022', width: 160, title: '项目名称',search: 'default'}
            , {field: 'GWCA040', width: 160, title: '合同总数'}
            , {field: 'GWCA041', width: 160, title: '合同金额'}
            , {field: 'GWGG004', width: 160, title: '已回款'}
            , {field: 'GWGG004', width: 160, title: '未回款'}
            , {field: 'GWCAORIU_NAME', width: 160, title: '申请人',search: 'default',searchOpts: {icon:true}}
            , {field: 'GWCAORIG_NAME', width: 160, title: '申请人组织'}
            , {field: 'GWCAACTI', width: 100, title: '状态',filterDatas:app.datas_("gwglacti")}
            ,{
                width: 60,
                title: '',
                toolbar: '#script-gwci001setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        cols_gwcb = [[
            {field: 'GWCBSEQ', width: 80, title: '序号'}
            , {field: 'GWCB002', width: 120, title: '产品编号'}
            , {field: 'GWAHL003', width: 80, title: '产品分类'}
            , {field: 'GWAOL003', width: 80, title: '产品系列'}
            , {field: 'GWCB003', width: 100, title: '产品厂家',
                filterDatas: app.datas_('crm_gwci001_gwcb003')
            }
            , {field: 'GWCB004', width: 100, title: '配置情况',
                filterDatas: app.datas_('crm_gwci001_gwcb004')
            }
            , {field: 'GWCB013', width: 100, title: '车辆配置明细'}
            , {field: 'GWCB005_NAME', width: 100, title: '计量单位'}
            , {field: 'GWCB006', width: 100, title: '数量'}
            , {field: 'GWCB008', width: 100, title: '含税单价'}
            , {field: 'GWCB010', width: 100, title: '含税总价'}
        ]],
        cols_gwcc = [[
            {field: 'GWCCSEQ', width: 80, title: '序号'}
            , {field: 'GWCC001', width: 120, title: '支付阶段', edit: 'select',
                filterDatas: app.datas_('crm_gwci001_gwcc001')
            }
            , {field: 'GWCC009', width: 120, title: '加计天数'}
            , {field: 'GWCC003', width: 140, title: '收款比例%'}
            , {field: 'GWCC004', width: 80, title: '收款金额'}
            , {field: 'GWCC008', width: 80, title: '备注'}
        ]],
        gwci001SettingAction,
        moduleName = '销售合同'
    ;

    var Gwci001 = app.Module.extend({
        init: function () {
            self = this;
            var _searchsArr = app.cloneJSON(cols[0]);
            self._super({
                _tag: 'gwci001',
                _searchs: [{
                    id: 'gwci001', tableId: 'gwci001_table', datas: cols[0]
                }],
                _loadTplsFull: [
                    {
                        dom: 'common_open_window', tpl: 'common/_open_window'
                    },
                    {
                        dom: 'gwci001_edit', tpl: 'gwci001_edit'
                    },
                    {
                        dom: 'checkApprovalProcess', tpl: 'checkApprovalProcess'
                    }
                ]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwci001_table'),
                    url: g_ctxPath + 'crm/gwci001/getGwcalist',
                    exportsUrl: g_ctxPath + 'crm/gwci001/getGwcalist',
                    exportsName: moduleName + '数据',
                    title: moduleName + '信息表',
                    method: 'post',
                    id: 'gwci001_table',
                    size: _layuiConfig.ui_size,
                    autoSort: false, //禁用前端自动排序
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: '',
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-gwci001setting-action').innerHTML).render({}, function (string) {
                    gwci001SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwci001_table)', function (obj) {

                    layer.tips(gwci001SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        self._openTplFull({
                                            tag: 'gwci001_edit',
                                            tagData: {
                                                obj: obj
                                            },
                                            index: 1,
                                            openOpts: {
                                                title: moduleName + '编辑',
                                                maxmin: true
                                            }
                                        });
                                        break;
                                    case '1':
                                        if(obj.data.GWCAACTI !== 'N' && obj.data.GWCAACTI !== 'R'){
                                            app.showWarning("N:未审核或R:拒审状态才可删除！");
                                            return;
                                        }
                                        self.delData(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行单击事件
                table.on('row(gwci001_table)', function (obj) {
                    table_bottom_where.GWCADOCNO = obj.data.GWCADOCNO;
                    obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
                    self.reloadGwcbTableCurr();
                    self.reloadGwccTableCurr();
                });

                //监听行双击事件
                table.on('rowDouble(gwci001_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwci001_edit',
                        tagData: {
                            obj: obj,
                            is_detail:true
                        },
                        index: 1,
                        openOpts: {
                            title: moduleName + '详情',
                            maxmin: true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwci001_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwci001_edit',
                                tagData: {
                                    is_add: true
                                },
                                index: 1,
                                openOpts: {
                                    title: moduleName + '编辑',
                                    maxmin: true
                                }
                            });
                            break;
                        case 'del':
                            self.delDataBatch();
                            break;
                        case 'commit' :
                            self.changeStatusBatch("commit");
                            break;
                        case 'unCommit' :
                            self.changeStatusBatch("unCommit");
                            break;
                        case 'checkProcess' : // 查看审批流程
                            self.checkProcess();
                            break;
                        case 'transferERP' : // 查看审批流程
                            self.transferERP();
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        initGwcbTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwci001_table_gwcb'),
                    url: g_ctxPath + 'crm/gwci001/getGwcbList',
                    exportsUrl: g_ctxPath + 'crm/gwci001/getGwcbList',
                    exportsName: '合同明细数据',
                    title: '合同明细信息表',
                    method: 'post',
                    id: 'gwci001_table_gwcb',
                    size: _layuiConfig.ui_size,
                    autoSort: false, //禁用前端自动排序
                    height: '400px',
                    cols: cols_gwcb,
                    page: true,
                    where: table_bottom_where,
                    done: function (res, curr, count) {

                    }
                });
            });
        },

        initGwccTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwci001_table_gwcc'),
                    url: g_ctxPath + 'crm/gwci001/getGwccList',
                    exportsUrl: g_ctxPath + 'crm/gwci001/getGwccList',
                    exportsName: '收款计划数据',
                    title: '收款计划信息表',
                    method: 'post',
                    id: 'gwci001_table_gwcc',
                    size: _layuiConfig.ui_size,
                    autoSort: false, //禁用前端自动排序
                    /*toolbar: '#detail-table-toolbar',
                     defaultToolbar: ['filter', 'exports'],*/
                    height: '400px',
                    cols: cols_gwcc,
                    page: true,
                    where: table_bottom_where,
                    done: function (res, curr, count) {

                    }
                });
            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwci001_table', where: table_where});
        },

        reloadGwcbTableCurr: function () {
            self._reloadTable({id: 'gwci001_table_gwcb', where: table_bottom_where});
        },

        reloadGwccTableCurr: function () {
            self._reloadTable({id: 'gwci001_table_gwcc', where: table_bottom_where});
        },

        delData: function (obj) {
            if (obj.data.GWCAACTI !== 'N' && obj.data.GWCAACTI !== 'R') {
                app.showWarning("只有未审核或拒审的资料才能删除");
                return;
            }
            app.confirm({
                msg: '是否删除选择的信息？',
                ok: function () {
                    app.ajax({
                        xurl: 'crm/gwci001/del',
                        xdata: obj.data,
                        loading: true,
                        okmsg: moduleName + '删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            });

        },

        delDataBatch: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwci001_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    if (v.GWCAACTI !== 'N' && v.GWCAACTI !== 'R') {
                        app.showWarning("N:未审核或R:拒审状态才可删除!");
                        flag = false;
                        return;
                    }else {
                        var _temp = app.fields_('GWCA_T');
                        _temp.GWCADOCNO = v.GWCADOCNO;
                        _temp.GWCAENT = v.GWCAENT;
                        _xdata.push(_temp);
                    }
                });
                if(flag) {
                    app.ajax({
                        xurl: 'crm/gwci001/delDataBatch',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        confirm:{msg: "是否删除选择的数据？"},
                        okmsg: moduleName + '删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            }
        },

        changeStatusBatch:function(action) {
            var checkStatus = table.checkStatus('gwci001_table');
            var flag = true;
            if(checkStatus.data.length === 0) {
                app.showWarning("至少选择一笔数据进行操作");
            }
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    switch (action) {
                        case "commit" :  // 提交
                            if( v.GWCAACTI === "N" || v.GWCAACTI === 'R'){
                                v.GWCAACTI = "T"
                            }
                            else {
                                app.showWarning("未审核或退回状态，才能进行提交操作");
                                flag = false;
                                return;
                            }
                            break;
                        case "unCommit" : // 取消提交
                            if(v.GWCAACTI !== "T") {
                                app.showWarning("已提交的资料才能取消提交");
                                flag = false;
                                return;
                            }else if(v.GWCA054 != null) {
                                app.showWarning("存在已建立ERP订单的合同" + v.GWCADOCNO + "，不可取消提交");
                                flag = false;
                                return;
                            }else {
                                v.GWCAACTI = "N"
                            }
                            break;
                    }
                    _xdata.push({
                        GWCADOCNO: v.GWCADOCNO,
                        GWCAACTI: v.GWCAACTI,
                        GWCAENT: v.GWCAENT,
                        GWCA056: v.GWCA056,
                        GWCA054: v.GWCA054
                    });
                });
                if(!flag){
                    return;
                }else {
                    app.ajax({
                        xurl: 'crm/gwci001/updateStatus',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        okmsg: moduleName +'状态修改成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            }
        },

        transferERP:function () {
            var checkStatus = table.checkStatus('gwci001_table');
            app.ajax({
                xurl: 'crm/gwci001/transferERP',
                xdata: {GWCADOCNO: checkStatus.data[0].GWCADOCNO, TYPE: '3' },
                loading: true,
                okmsg: moduleName +'状态修改成功',
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTableCurr();
                    }
                }
            });
        },

        checkProcess:function() {
            var checkStatus = table.checkStatus('gwci001_table');
            if(checkStatus.data.length === 0) {
                app.showWarning("请选择一笔数据进行操作");
                return;
            }
            if(checkStatus.data.length > 1) {
                app.showWarning("只能选择一笔数据进行操作");
                return;
            }
            let data = checkStatus.data[0];
            if(data.GWCAACTI != 'T') {
                if(checkStatus.data.length > 1) {
                    app.showWarning("已提交的资料才能查看审批流程");
                    return;
                }
            }

            self._openTplFull({
                tag: 'checkApprovalProcess',
                tagData: {
                    obj: {
                        fd_test_id:"16ebfe3f1291e078dae07564bc5aaa7b",
                        fd_formal_id:"16f02359825062d41182a924b959efc4",
                        fd_process_id:data.GWCA056
                    }
                },
                index: 2,
                openOpts: {
                    title: "审批流程",
                    area: ['800px','400px']
                }
            });
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: vueData,
                    searchId: 'gwci001',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if(field == 'GWCA004'){ //招标编号
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwga001',
                                            columnList: [
                                                {label: '招标编号', prop: 'GWGA001', width: '150'},
                                                {label: '项目名称', prop: 'GWGA002', width: '150'},
                                                {label: '客户', prop: 'GWAKL004', width: '150'},
                                                {label: '开标时间', prop: 'GWAKL004', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWCA004 = data.GWGA001;
                                        }
                                    },
                                    index: 0,
                                    openOpts: {
                                        title: '选择招标信息',
                                        area: ['650px', '465px']
                                    }
                                });
                            }else if (field == 'GWCA005') { //合同
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwca005',
                                            columnList: [
                                                {label: '合同编号', prop: 'GWCA005', width: '150'},
                                                {label: '合同签订组织', prop: 'GWCAMLEGAL', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWCA005 = data.GWCA005;
                                        }
                                    },
                                    index: 0,
                                    openOpts: {
                                        title: '选择合同编号',
                                        area: ['600px', '465px']
                                    }
                                });
                            } else if(field == 'GWCAORIU_NAME'){
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_sysuser_id',
                                            columnList: [
                                                {label: '账号', prop: 'ACCOUNT', width: '150'},
                                                {label: '姓名', prop: 'NAME', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWCAORIU_NAME = data.ID;
                                        }
                                    },
                                    index: 0,
                                    openOpts: {
                                        title: '选择申请员',
                                        area: ['600px', '470px']
                                    }
                                });
                            } else if(field == 'GWCA055'){
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_gwak001',
                                            columnList: [
                                                {label: '客户编号', prop: 'GWAK001', width: '150'},
                                                {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                                {label: '客户繁称', prop: 'GWAKL004', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWCA055 = data.GWAK001;
                                        }
                                    },
                                    index: 0,
                                    openOpts: {
                                        title: '选择客户单位',
                                        area: ['600px', '470px']
                                    }
                                });
                            }
                        }
                    }
                }]
            });
            self.initTable();
            self.initGwcbTable();
            self.initGwccTable();
        },
        _lazy: function () {
            this._super();
        }
    });

    return new Gwci001();

});